using System;
using System.Web.UI;
using Framework.Library.Utilities;
using Framework.Module.Logging.Statistic;

namespace Framework.Module.Logging
{
    public partial class LogZone : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            int zoneid = ConvertUtility.ToInt32(Request.QueryString["logzoneid"]);
            int distid = ConvertUtility.ToInt32(Request.QueryString["logdistid"]);
            if (zoneid > 0)
            {
                int day = DateTime.Now.Day;
                int month = DateTime.Now.Month;
                int year = DateTime.Now.Year;
                //int ZoneID ;
                //try
                //{
                //    ZoneID = ConvertUtility.ToInt32(Request.QueryString["catid"]);
                //}
                //catch
                //{
                //    ZoneID = zoneid;
                //}
                StatisticZoneInfo stainfo = new StatisticZoneInfo();
                try
                {
                    int statisticid = GetStatisticID(zoneid, day, month, year);
                    if (statisticid > 0)
                    {
                        stainfo.StaticZoneID = statisticid;
                        stainfo.Counter = 1;
                        stainfo.Datetime = DateTime.Now;
                        StatisticZoneDB.Update(stainfo);
                    }
                    else
                    {
                        stainfo.ZoneID = zoneid;
                        stainfo.Counter = 1;
                        stainfo.Datetime = DateTime.Now;
                        StatisticZoneDB.Insert(stainfo);
                    }
                }
                catch
                {
                    stainfo.ZoneID = zoneid;
                    stainfo.Counter = 1;
                    stainfo.Datetime = DateTime.Now;
                    StatisticZoneDB.Insert(stainfo);
                }
            }
            else
            {
                if(distid > 0)
                {
                    int ZoneID = 0;
                    //int ZoneID = DistributionDB.GetZoneID(distID);
                    int day = DateTime.Now.Day;
                    int month = DateTime.Now.Month;
                    int year = DateTime.Now.Year;
                    StatisticZoneInfo stainfo = new StatisticZoneInfo();

                    try
                    {
                        int statisticid = StatisticZoneDB.GetIDbyDate(zoneid, day, month, year);
                        if (statisticid > 0)
                        {
                            stainfo.StaticZoneID = statisticid;
                            stainfo.Counter = 1;
                            stainfo.Datetime = DateTime.Now;
                            StatisticZoneDB.Update(stainfo);
                        }
                        else
                        {
                            stainfo.ZoneID = zoneid;
                            stainfo.Counter = 1;
                            stainfo.Datetime = DateTime.Now;
                            StatisticZoneDB.Insert(stainfo);
                        }
                    }
                    catch
                    {
                        stainfo.ZoneID = zoneid;
                        stainfo.Counter = 1;
                        stainfo.Datetime = DateTime.Now;
                        StatisticZoneDB.Insert(stainfo);
                    }  
                }
                else{}
            }
        }
        private int GetStatisticID(int zoneid, int day, int month, int year)
        {
            int statisticid = ConvertUtility.ToInt32(Cache["Statistic_" + zoneid + "_" + day + "_" + "month" + "_" + year]);
            if (statisticid == 0)
            {
                statisticid = StatisticZoneDB.GetIDbyDate(zoneid, day, month, year);
                Cache["Statistic"] = statisticid;
            }
            return statisticid;
        }
    }
}
